import base64
import hashlib
import hmac
import time
import urllib

from com.util.httpRequest import HttpRequest


# 默认推送到钉钉测试群配置
secret = 'SEC0e7a3af154632150138644c81dee213b56cdfeac7a463736c4607c85964d40c0'
secret_enc = secret.encode('utf-8')




def pushDingDingToWogoo(content):
    # 关键词 = 吾股 , content必须包含"吾股"
    url = "https://oapi.dingtalk.com/robot/send?access_token=a94fd09fe30a821e00ec0737279afca0d978444371edb50cc547a8ad832213ee"
    header = {
        "Content-Type": "application/json; charset=utf-8",
        }
    body = {
        "msgtype": "text",
        "text": {"content": content},
        "at": {
            "atMobiles":[
                "18838978084"
            ]
        }
    }
    res = HttpRequest().http_request(method='POST', url=url, jsonp=body, headers=header)
    return res


def pushDingDing(content):
    timestamp = str(round(time.time() * 1000))
    string_to_sign = '{}\n{}'.format(timestamp, secret)
    string_to_sign_enc = string_to_sign.encode('utf-8')
    hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
    sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
    url = f"https://oapi.dingtalk.com/robot/send?" \
                 f"access_token=9554081f47f678ca137fbf94c52dde34830732b87cb047b5c4e54e301dc43e82" \
                 f"&timestamp={timestamp}&sign={sign}"

    header = {
        "Content-Type": "application/json; charset=utf-8",
    }
    body = {
        "msgtype": "text",
        "text": {"content": content},
    }
    res = HttpRequest().http_request(method='POST', url=url, jsonp=body, headers=header)
